﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using KioskViewerBLL.BLLObjects;
using KioskViewerDAL.DataObjects;


namespace KioskViewerDAL.SqlServer
{
    /// <summary>
    /// Sql Server specific data access object that handles data access
    /// of customers.
    /// </summary>
    public class SqlServerMediaSearch : IMediaSearch
    {

        /// <summary>
        /// media type=2
        /// </summary>
        /// <param name="TitleKeyword"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="TotalRecords"></param>
        /// <param name="TotalPages"></param>
        /// <returns></returns>
        public IList<MediaSearch> SearchBackgound(string TitleKeyword,
                                                        int CategoryId,
                                                     int PageIndex,
                                                     int PageSize,
                                                    ref int TotalRecords,
                                                    ref int TotalPages)
        {
            PageSize = PageSize < 1 ? 10 : PageSize;
            PageIndex = PageIndex < 1 ? 1 : PageIndex;

            string sErr = string.Empty;
            // only search for active background
            DataSet ds = Untils.ExecuteReader("dbo.[kioskviewer_media_getbackground_listbycategory]", ref sErr, TitleKeyword,CategoryId, PageIndex, PageSize, 200);
            DataTable dt = ds.Tables[0];

            TotalRecords = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            TotalPages = (int)(TotalRecords / PageSize) + (((TotalRecords % PageSize) == 0) ? 0 : 1);

            IList<MediaSearch> list = new List<MediaSearch>();
            if (sErr != string.Empty) return list;
            foreach (DataRow row in dt.Rows)
            {
                MediaSearch ms = new MediaSearch();
                ms.Media_id = new Guid(row["media_id"].ToString());
                ms.Title = row["title"].ToString();
                ms.Catalog_id = Convert.ToString(row["catalog_id"].ToString());
                ms.Description = Convert.ToString(row["description"].ToString());
                ms.Media_types = row["media_types"].ToString();
                ms.Artist = row["artist"].ToString();
                ms.Filename = row["filename"].ToString();
                ms.Added = Convert.ToDateTime(Null.SetNull(row["added"], ms.Added));
                ms.Updated = Convert.ToDateTime(Null.SetNull(row["updated"], ms.Updated));
                //ms.Physical_path = row["physical_path"].ToString();
                ms.Media_code = row["media_code"].ToString();
                ms.Track_length = row["track_length"].ToString();

                list.Add(ms);
            }
            return list;
        }
    }
}
